(function() {
  var express, fs, iced, mkdirp, moment, path, pgsql, router, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  moment = require('moment');

  fs = require('fs');

  mkdirp = require('mkdirp');

  path = require('path');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, done, end_date, err, result, start_date, table_name, today, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.stat_title = '交易按类型汇总';
    where_clause = "(\n    trinfo::json#>>'{\"code\"}' like $1 or\n    trinfo::json#>>'{\"name\"}' like $1\n)";
    today = moment().format('YYYY-MM-DD');
    start_date = req.body.start_date || req.query.start_date || today;
    end_date = req.body.end_date || req.query.end_date || today;
    where_clause += " and sys_time::date >= '" + start_date + "'";
    res.locals.start_date = start_date;
    where_clause += " and sys_time::date <= '" + end_date + "'";
    res.locals.end_date = end_date;
    table_name = 'pay_trlist_hist';
    if (start_date === today && end_date === today) {
      table_name = 'pay_trlist';
    }
    res.locals.succed = req.body.succed || req.query.succed || 'true';
    if (res.locals.succed !== 'none') {
      if (res.locals.succed === 'true') {
        where_clause += " and status = 0 and\ncase\nwhen chnl in (1,4) then\n    ( o_pkt is not null and\n    json_extract_path_text(o_pkt, '39') = '00')\nwhen chnl = 3 then\n    ( n_pkt is not null and\n    json_extract_path_text(n_pkt, 'respCode') = '00')\nend";
      } else {
        where_clause += " and (status <> 0 or\ncase\nwhen chnl in (1,4) then\n    ( o_pkt is null or\n    json_extract_path_text(o_pkt, '39') <> '00')\nwhen chnl = 3 then\n    ( n_pkt is null or\n    json_extract_path_text(n_pkt, 'respCode') <> '00')\nend)";
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/trcodes.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 63
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/trcodes.coffee"
          });
          client.query("select chnl, json_extract_path_text(trinfo, 'code') as trcode, json_extract_path_text(trinfo, 'name') as trname, count(*) as total_cnt, round(sum(coalesce(fee, 0.0)), 2) as total_fee, case when chnl in (1,4) then round(sum(cast( json_extract_path_text(i_pkt,'4') as decimal)) / 100, 2) when chnl = 3 then round(sum(cast( json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100, 2) end as total_amt from " + table_name + " where " + where_clause + " and incoming = false and json_extract_path_text(trinfo, 'settl') = 'true' group by chnl, trcode, trname order by chnl, trcode", ["%" + res.locals.keyword + "%"], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 89
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          res.locals.records = result.rows;
          res.locals.total = res.locals.records.length;
          return res.render('trstat/trcodes');
        });
      };
    })(this));
  });

}).call(this);
